package com.qupai.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qupai.constant.MessageConstant;
import com.qupai.constant.StatusConstant;
import com.qupai.dto.PtActivityDTO;
import com.qupai.dto.PtActivityPageQueryDTO;
import com.qupai.entity.PtActivity;
import com.qupai.entity.User;
import com.qupai.exception.DeletionNotAllowedException;
import com.qupai.mapper.PtActivityMapper;
import com.qupai.mapper.UserMapper;
import com.qupai.result.PageResult;
import com.qupai.service.PtActivityService;
import com.qupai.service.PtRegistrationService;
import com.qupai.vo.PtActivityVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
@RequiredArgsConstructor
public class PtActivityServiceImpl extends ServiceImpl<PtActivityMapper, PtActivity> implements PtActivityService {

    private final PtActivityMapper activitiesMapper;
    private final UserMapper userMapper;
    private final PtRegistrationService ptRegistrationService;

    /**
     * 新增活动
     */
    @Override
    public void save(PtActivityDTO activitiesDTO) {
        PtActivity activities=new PtActivity();
        BeanUtils.copyProperties(activitiesDTO,activities);
        activities.setStatus(StatusConstant.DISABLE);

        activitiesMapper.insert(activities);
    }


    /**
     * 活动分页查询
     */
    @Override
    public PageResult pageQuery(PtActivityPageQueryDTO activitiesPageQueryDTO) {

        // 1.构建分页条件
        Page<PtActivity> page=activitiesPageQueryDTO.toMpPageDefaultSortByCreateTimeDesc();

        // 2.分页查询
        Page<PtActivity> p=lambdaQuery()
                .like(activitiesPageQueryDTO.getName()!=null,PtActivity::getName,activitiesPageQueryDTO.getName())
                .eq(activitiesPageQueryDTO.getCollegesId()!=null,PtActivity::getCollegesId,activitiesPageQueryDTO.getCollegesId())
                .eq(activitiesPageQueryDTO.getStatus()!=null,PtActivity::getStatus,activitiesPageQueryDTO.getStatus())
                .page(page);

        // 3.调用插件获得数据
        //获得总的记录数
        long total = p.getTotal();
        //获得总的页数
        long pages = p.getPages();
        //获得当前页数据集合
        List<PtActivity> records = p.getRecords();

        return new PageResult(total,pages,records);
    }

    /**
     * 删除活动
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        for (Long id : ids) {
            PtActivity activities=activitiesMapper.selectById(id);
            //举办中的活动不能删除
            if (activities.getStatus().equals(StatusConstant.ENABLE) ){
                throw new DeletionNotAllowedException(MessageConstant.ACTIVITIES_ON_HOLD);
            }
        }
        activitiesMapper.deleteBatchIds(ids);
    }

    /**
     * 根据id查询活动
     */
    @Override
    public PtActivityVO getById(Long id) {
        PtActivity activities=activitiesMapper.selectById(id);
        PtActivityVO activitiesVO=new PtActivityVO();
        BeanUtils.copyProperties(activities,activitiesVO);
        return activitiesVO;
    }

    /**
     *  修改活动
     */
    @Override
    public void update(PtActivityDTO activitiesDTO) {
        PtActivity activities=new PtActivity();
        BeanUtils.copyProperties(activitiesDTO,activities);
        activitiesMapper.updateById(activities);

    }

    /**
     *  启用或禁用活动
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        PtActivity activities = PtActivity.builder()
                .id(id)
                .status(status)
                .build();
        activitiesMapper.updateById(activities);
    }

    /**
     * 根据各分类查询活动列表
     * @param activities@return
     */
    @Override
    public List<PtActivityVO> list(PtActivity activities) {
        List<PtActivity>activitiesList=activitiesMapper.list(activities);
        List<PtActivityVO>activitiesVOList=new ArrayList<>();
        for (PtActivity activity:activitiesList){
            PtActivityVO activitiesVO=new PtActivityVO();
            //属性拷贝
            BeanUtils.copyProperties(activity,activitiesVO);
            activitiesVOList.add(activitiesVO);
        }
        return activitiesVOList;
    }

    //开启事务
    @Transactional
    @Override
    public void givecoin(Long id) {
        //获取报名人员
        List<User> users = ptRegistrationService.getRegistrationsByActivityId(id);
        users.forEach(user -> {
            BigDecimal walletCommon = user.getWalletCommon();
            BigDecimal activityCoins = activitiesMapper.selectById(id).getActivityCoins();
            BigDecimal newBalance = walletCommon.add(activityCoins);
            user.setWalletCommon(newBalance);
            userMapper.updateById(user);
        });
    }
}
